// pages/cartoons/cartoons.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    youngboy: {},
    youngman: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var youngboyUrl = app.globalData.youyaoqibase + "/youngboy";
    //青年漫画
    var youngmanUrl = app.globalData.youyaoqibase + "/youngman"
//调用 因为这里是异步，所有每一次输出的顺序都不一样
    this.getmanhuaListData(youngboyUrl, "youngboy","少年漫画");
    this.getmanhuaListData(youngmanUrl, "youngman","青年漫画")
  },
  //列表数据
  getmanhuaListData: function (url, setteKey,cetegoryTitle) {
    var that = this;
    wx.request({
      url: url,
      method: 'GET',
      //数据处理
      success: function (res) {
        that.processManhuaData(res.data.result.bookList, setteKey, cetegoryTitle)
      }
    })
  },
  // 处理漫画列表需要的数据
  processManhuaData: function (manhuaData, settedKey,cetegoryTitle) {
    var manhuasplice = manhuaData.splice(0, 3);
    var cartoons = [];
    for (var idx in manhuasplice) {
      var subject = manhuasplice[idx];
 
      var title = subject.name;
      if (title.length >= 6) {
        title = title.substring(0, 6) + "...";
      }
      var temp = {
        title: title,
        coverImg: subject.coverImg,
        cartoonsid: subject.postid
      }
      cartoons.push(temp)
      // console.log(cartoons)
      // this.setData({
      //   cartoons:cartoons
      // }) 
      var readyData = {};
      
      // readyData[settedKey] = cartoons;
      readyData[settedKey] = {
        cetegoryTitle:cetegoryTitle,
        cartoons:cartoons
      };
      this.setData(readyData)
    }
  },
  //获得更多的方法
  onMoreCartoonTap:function(event){
    var category = event.currentTarget.dataset.category;
    wx.navigateTo({
      url: 'more-cartoon/more-cartoon?category=' + category,
  
    })
  },
  // 获得漫画详情页
  onCartoonsTap:function(event){
    var cartoonsId = event.currentTarget.dataset.cartoonsid;   
    wx.navigateTo({
      url: "cartoon-detail/cartoon-detail?id="+ cartoonsId
    })
  }









})